package westh.ilib.service;

import java.sql.*;

import net.sf.json.JSONObject;
import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.config.Config;

/**
 * @author gaoyang
 *
 */

public class UserService {
	private final static Log log = LogFactory.getLog(UserService.class);
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");
	
	public String getUserDomain(String userId) {
//		String url = "jdbc:mysql://localhost:3306/ilib";
//		String user = "root";
//		String password = "root";
//		String driver = "com.mysql.jdbc.Driver";
//		JSONArray keywords = new JSONArray();
//		try {
//			Class.forName(driver);
//			Connection con = DriverManager.getConnection(url, user, password);
//			Statement statement = con.createStatement();
//			String sql = "select field from customer where userName = '" + userId + "';";
//			ResultSet rs = statement.executeQuery(sql);
//			while(rs.next()) {
//				JSONObject temp = new JSONObject();
//				temp.put("keyword", rs.getString("field"));
//				keywords.add(temp);
//			}
//			rs.close();
//			con.close();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		
		JSONObject inStr = new JSONObject();       // package the parameters got from front stage
		JSONObject inJson = new JSONObject();      // package the data to send to back stage
		String methodName = "getCustomerList";     // method name in back stage called by mule
		inStr.put("customerJson", "22");
		inJson.put("methodName", methodName);
		inJson.put("jsonStr", inStr);
		String inputStr = inJson.toString();       // convert the inJson into string to send
		log.debug(inputStr);
		
		/* get jSon information start */
		String outString = "";                     // get resource id list result output by mule
		try {
			MuleClient client = new MuleClient();
			//call WebService
			UMOMessage umo = client.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=rulesControl", inputStr, null);
			outString = (String)umo.getPayload();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		/* get jSon information end */
		
		String userDomain = "[]";
		
		JSONObject jsonOut = JSONObject.fromObject(outString);
		int retCode = jsonOut.getInt("retCode");
		if (retCode == 0) {
			JSONArray keywords = new JSONArray();
			JSONArray result = jsonOut.getJSONArray("result");
			int n = result.size();
			for (int i = 0; i< n; i++) {
				JSONObject customer = result.getJSONObject(i);
				String id = customer.getString("id");
				if (userId.equals(id)) {
					String[] temp = customer.getString("field").split("[ ]+");
					for (int j = 0; j < temp.length; j++) {
						JSONObject word = new JSONObject();
						word.put("keyword", temp[j]);
						keywords.add(word);
					}
					
				}
			}
			userDomain = keywords.toString();
		}
		
		return userDomain;
	}
}
